<!DOCTYPE HTML>
<html>
<!--
Bug 1022797 - Settings support from WebIDE
-->
<head>
  <meta charset="utf-8">
  <title>Test Settings Actor</title>
  <script type="text/javascript" src="chrome://mochikit/content/MochiKit/MochiKit.js"></script>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script>

function runTests() {
  var Cu = Components.utils;
  var Cc = Components.classes;
  var Ci = Components.interfaces;

  var {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
  var {DebuggerClient} = require("devtools/shared/client/main");
  var {DebuggerServer} = require("devtools/server/main");

  if (SpecialPowers.isMainProcess()) {
    Cu.import("resource://gre/modules/SettingsRequestManager.jsm");
  }

  SimpleTest.waitForExplicitFinish();

  var {getSettingsFront} = require("devtools/shared/fronts/settings");
  var {_setDefaultSettings} = require("devtools/server/actors/settings");

  DebuggerServer.init(function () { return true; });
  DebuggerServer.addBrowserActors();

  var client = new DebuggerClient(DebuggerServer.connectPipe());
  client.connect().then(function onConnect() {
    client.listTabs(function onListTabs(aResponse) {
      var s = getSettingsFront(client, aResponse);

      var settings = {};
      var resetSettings = {};
      var fakeSettings = {
        "wifi.enabled": true,
        "audio.volume.alarm": 15,
        "app.reportCrashes": "ask",
        "app.someObject": { active: true }
      };
      var localSetting = {
        "wifi.enabled": false,
        "audio.volume.alarm": 0,
        "app.reportCrashes": "none",
        "app.someObject": {}
      };

      function checkValues() {
        is(settings.allSettings["wifi.enabled"].hasUserValue, false, "original unchanged bool setting");
        is(settings.allSettings["audio.volume.alarm"].hasUserValue, false, "original unchanged int setting");
        is(settings.allSettings["app.reportCrashes"].hasUserValue, false, "original unchanged string setting");
        is(settings.allSettings["app.someObject"].hasUserValue, false, "original unchanged object setting");

        is(settings.allSettings["wifi.enabled"].value, fakeSettings["wifi.enabled"], "original read/write bool setting");
        is(settings.allSettings["audio.volume.alarm"].value, fakeSettings["audio.volume.alarm"], "original read/write int setting");
        is(settings.allSettings["app.reportCrashes"].value, fakeSettings["app.reportCrashes"], "original read/write string setting");
        is(JSON.stringify(settings.allSettings["app.someObject"].value), JSON.stringify(fakeSettings["app.someObject"]), "original read/write object setting");

        is(settings.allUpdatedSettings["wifi.enabled"].hasUserValue, true, "updated user-changed bool setting");
        is(settings.allUpdatedSettings["audio.volume.alarm"].hasUserValue, true, "updated user-changed int setting");
        is(settings.allUpdatedSettings["app.reportCrashes"].hasUserValue, true, "updated user-changed string setting");
        is(settings.allUpdatedSettings["app.someObject"].hasUserValue, true, "updated user-changed object setting");

        is(settings["wifi.enabled"], localSetting["wifi.enabled"], "updated bool setting");
        is(settings["audio.volume.alarm"], localSetting["audio.volume.alarm"], "updated int setting");
        is(settings["app.reportCrashes"], localSetting["app.reportCrashes"], "updated string setting");
        is(JSON.stringify(settings["app.someObject"]), JSON.stringify(localSetting["app.someObject"]), "updated object as string setting");

        is(resetSettings["wifi.enabled"], fakeSettings["wifi.enabled"], "reset to original bool setting");
        is(resetSettings["audio.volume.alarm"], fakeSettings["audio.volume.alarm"], "reset to original int setting");
        is(resetSettings["app.reportCrashes"], fakeSettings["app.reportCrashes"], "reset to original string setting");
        is(JSON.stringify(resetSettings["app.someObject"]), JSON.stringify(fakeSettings["app.someObject"]), "reset to original object setting");

        client.close().then(() => {
          DebuggerServer.destroy();
          SimpleTest.finish();
        });
      }

      // settings.json doesn't exist outside of b2g so we will fake it.
      _setDefaultSettings(fakeSettings);
      s.setSetting("wifi.enabled", fakeSettings["wifi.enabled"])
      .then(() => s.setSetting("audio.volume.alarm", fakeSettings["audio.volume.alarm"]))
      .then(() => s.setSetting("app.reportCrashes", fakeSettings["app.reportCrashes"]))
      .then(() => s.setSetting("app.someObject", fakeSettings["app.someObject"]))
      .then(() => s.getAllSettings().then(json => settings.allSettings = json))
      .then(() => s.setSetting("wifi.enabled", localSetting["wifi.enabled"]))
      .then(() => s.setSetting("audio.volume.alarm", localSetting["audio.volume.alarm"]))
      .then(() => s.setSetting("app.reportCrashes", localSetting["app.reportCrashes"]))
      .then(() => s.setSetting("app.someObject", localSetting["app.someObject"]))
      .then(() => s.getAllSettings().then(json => settings.allUpdatedSettings = json))
      .then(() => s.getSetting("wifi.enabled")).then(value => settings["wifi.enabled"] = value)
      .then(() => s.getSetting("audio.volume.alarm")).then(value => settings["audio.volume.alarm"] = value)
      .then(() => s.getSetting("app.reportCrashes")).then(value => settings["app.reportCrashes"] = value)
      .then(() => s.getSetting("app.someObject")).then(value => settings["app.someObject"] = value)
      .then(() => s.clearUserSetting("wifi.enabled")).then(() => {
        s.getSetting("wifi.enabled").then(value => resetSettings["wifi.enabled"] = value);
      })
      .then(() => s.clearUserSetting("audio.volume.alarm")).then(() => {
        s.getSetting("audio.volume.alarm").then(value => resetSettings["audio.volume.alarm"] = value);
      })
      .then(() => s.clearUserSetting("app.reportCrashes")).then(() => {
        s.getSetting("app.reportCrashes").then(value => resetSettings["app.reportCrashes"] = value);
      })
      .then(() => s.clearUserSetting("app.someObject")).then(() => {
        s.getSetting("app.someObject").then(value => {
          resetSettings["app.someObject"] = value
        }).then(checkValues);
      });
    });
  });
}

window.onload = function () {
  runTests();
}
</script>
</pre>
</body>
</html>
